package com.chainyoung.component_tmtrade.ui.fragment.c2c;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.bean.CurrencyMarketList;
import com.chainyoung.component_tmtrade.di.component.DaggerTradeCurrencyActivityComponent;
import com.chainyoung.component_tmtrade.di.component.DaggerTradeCurrencyComponent;
import com.chainyoung.component_tmtrade.di.module.TradeCurrencyActivityModule;
import com.chainyoung.component_tmtrade.di.module.TradeCurrencyModule;
import com.chainyoung.component_tmtrade.ui.activity.TradeCurrencyMarketListActivity;
import com.chainyoung.component_tmtrade.ui.adapter.TradeCoupleAdapter;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeCurrencyCoupleListContract;
import com.chainyoung.component_tmtrade.ui.mvp.presenter.TradeCurrencyCoupleListPresenter;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TreeMap;

import butterknife.BindView;

import static com.chainyoung.common.constant.CommonStringConstants.TRADE_NAME;
import static com.chainyoung.common.constant.CommonStringConstants.TRADE_TYPE;
import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyMainFragment.TYPE_BUY;

//选择交易对

public class TradeCurrencyListFragment extends BaseFragment<TradeCurrencyCoupleListPresenter> implements TradeCurrencyCoupleListContract.View {


    @BindView(R2.id.grid_view)
    GridView gridView;


    private String currencyName;
    private TradeCoupleAdapter mAdapter;
    private List<CurrencyMarketList.MarketListBean> mData = new ArrayList<>();

    public static TradeCurrencyListFragment newInstance(String currency) {
        TradeCurrencyListFragment fragment = new TradeCurrencyListFragment();
        Bundle args = new Bundle();
        args.putString("currencyName", currency);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        currencyName = getArguments().getString("currencyName");
    }


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerTradeCurrencyComponent.
                builder().
                appComponent(appComponent)
                .tradeCurrencyModule(new TradeCurrencyModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUi() {
        super.initUi();
        mAdapter = new TradeCoupleAdapter(mContext, mData);
        gridView.setAdapter(mAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CurrencyMarketList.MarketListBean item = (CurrencyMarketList.MarketListBean) mAdapter.getItem(position);
                if (((TradeCurrencyMarketListActivity) getActivity()).isFromPurse()) {
                    RouterUtils.getInstance()
                            .build(RouterConstants.PATH_MAIN_MARKET_ACTIVITY)
                            .withInt("page", 1)
                            .withInt(TRADE_TYPE, TYPE_BUY)
                            .withString(TRADE_NAME, item.getName())
                            .navigation(mContext);
                } else {
                    Intent data = new Intent();
                    data.putExtra("marketName", item.getName());
                    Objects.requireNonNull(getActivity()).setResult(Activity.RESULT_OK, data);
                    getActivity().finish();
                }
            }
        });
    }

    @Override
    protected void initData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("region", currencyName.toLowerCase());
        mPresenter.getMarketCurrencys(map);
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmtrade_activity_trade_couple_list;
    }


    @Override
    public void showMarketCurrencyCouples(CurrencyMarketList data) {
        mData.clear();
        mData.addAll(data.getMarketList());
        mAdapter.notifyDataSetChanged();
    }


}
